package com.xiangxiao.rpan.storage.core;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * @auther xiangxiao
 * @email 573768011@qq.com
 * @data 2023/8/25 16:13
 * 直接传输文件方式选择器
 */
@Component(value = "transferFileByDirectToolSelector")
public class TransferFileByDirectToolSelector implements ApplicationContextAware {
  private ApplicationContext applicationContext;

  private TransferFileByDirectTool transferFileByDirectTool;

  @Value("${rpan.storage.direct.selector}")
  private Class<? extends TransferFileByDirectTool> transferFileSelector;

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
    if (Objects.isNull(transferFileSelector)) {
      throw new RuntimeException("the storage processor type can not be null!<rpan.storage.processor.type>");
    }
    this.transferFileByDirectTool = this.applicationContext.getBean(transferFileSelector);
    if (Objects.isNull(this.transferFileByDirectTool)) {
      throw new RuntimeException("the storage processor type:" + transferFileSelector.getSimpleName() + " can not be found!");
    }
  }

  /**
   * 选择TransferFileByDirectTool实现类
   * @return
   */
  public TransferFileByDirectTool select() {
    return transferFileByDirectTool;
  }
}
